﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;

namespace DAL
{
    public class UserDAL
    {

        /// <summary>
        /// 增加一行用户数据
        /// </summary>
        /// <returns></returns>
        public bool AddUser(UserInfo user)
        {
            try
            {
                if (DBHelper.ExecuteSQL($"insert UserInfo(UserName,Account,Pwd,Phone,Email,User_Money,User_HeadPic,User_State) values('{user.UserName}','{user.Account}','{user.Pwd}','{user.Phone}','{user.Email}','{user.User_Money}','{user.User_HeadPic}','{user.User_State}')")<1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 执行查询，获取所有用户数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetUserList()
        {
            try
            {
                return DBHelper.ExucuteSQL_DataSet("select * from UserInfo");
            }
            catch (Exception)
            {

                return null;
            }
        }

        /// <summary>
        /// 执行查询，判断密码是否正确，根据用户ID获取用户数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetUserList(UserInfo user)
        {
            
            try
            {
                DataTable dt = DBHelper.ExucuteSQL_DataSet($"select * from UserInfo where (Account='{user.Account}' or Phone='{user.Phone}' or Email='{user.Email}') and Pwd='{user.Pwd}'").Tables[0];
                if (dt.Rows.Count==0)
                {
                    return null;
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }
        

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="user">传入修改后的密码和用户id</param>
        /// <returns></returns>
        public bool UpUserPwd(UserInfo user)
        {
            try
            {
                if (DBHelper.ExecuteSQL($"update UserInfo set Pwd='{user.Pwd}' where Email='{user.Email}'") < 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="user">传入用户实体类，主要id</param>
        /// <returns></returns>
        public bool UpUser(UserInfo user)
        {
            try
            {
                if (DBHelper.ExecuteSQL($"update UserInfo set " +
                    $"CardID='{user.CardID}',UserName='{user.UserName}',Name='{user.Name}',Address='{user.Address}',BirthDay='{user.BirthDay}',City='{user.City}', " +
                    $"where UserID='{user.UserID}'") < 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 用户管理，改变用户状态
        /// </summary>
        /// <param name="user">传入用户实体类，主要id</param>
        /// <returns></returns>
        public bool AdminUser(UserInfo user)
        {
            try
            {
                    if (DBHelper.ExecuteSQL($"update UserInfo set " +
    $"User_State={user.User_State} " +
    $"where UserID='{user.UserID}'") < 1)
                    {
                        return false;
                    }
                    return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
    }
}
